﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mysoft.ServerMonitoring.OMKit.Infrastructure.Events;
using ProjTemp.Desktop.Common.ViewModel;
using System.ComponentModel.Composition;
using ProjTemp.Common.Logging;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.ServiceLocation;
using System.Windows.Media;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using ProjTemp.Common;
using ProjTemp.Desktop.Common;

namespace Mysoft.ServerMonitoring.OMKit.HomePage.ViewModels
{
    [Export(typeof(HomePageViewModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    internal class HomePageViewModel : VMBase<ClientServerInfo>
    {
        [ImportingConstructor]
        public HomePageViewModel(ILogger logger, IRegionManager regionManager, IEventAggregator eventAggregator, IServiceLocator serviceLocator)
            : base(logger, regionManager, eventAggregator, serviceLocator)
        {
            this._logger.LogType = typeof(HomePageViewModel);

            this._eventAggregator.GetEvent<ClientServerSelected>().Subscribe(new Action<ClientServer>(ClientServerSelectedHandler), ThreadOption.UIThread);
            this._logger.Debug(GlobalConst.ObjectConstructed);
        }

        private void ClientServerSelectedHandler(ClientServer clientServer)
        {
            this.IsBusy = true;

            var clientServerInfo = new ClientServerInfo() { BasicServerInfo = clientServer };
            var foundIcons = new List<MetroIcon>();
            var dict = new ResourceDictionary { Source = new Uri(StyleConst.IconsUriAddress) };
            foundIcons.Add(new MetroIcon("服务监控", (Visual)dict["appbar_home"], 0));
            foundIcons.Add(new MetroIcon("WEB站点监控", (Visual)dict["appbar_monitor"], 1));
            foundIcons.Add(new MetroIcon("数据库监控", (Visual)dict["appbar_people_status"],1));
            foundIcons.Add(new MetroIcon("数据库健康巡检", (Visual)dict["appbar_alert"], 1));
            foundIcons.Add(new MetroIcon("日志采集", (Visual)dict["appbar_email_hardedge"],2));
            foundIcons.Add(new MetroIcon("磁盘监控", (Visual)dict["appbar_cloud_upload"],0));
            foundIcons.Add(new MetroIcon("性能监控", (Visual)dict["appbar_graph_line_up"],2));
            foundIcons.Add(new MetroIcon("服务监控", (Visual)dict["appbar_home"], 1));
            foundIcons.Add(new MetroIcon("WEB站点监控", (Visual)dict["appbar_monitor"], 0));
            foundIcons.Add(new MetroIcon("数据库监控", (Visual)dict["appbar_people_status"], 2));
            foundIcons.Add(new MetroIcon("数据库健康巡检", (Visual)dict["appbar_alert"], 1));
            foundIcons.Add(new MetroIcon("日志采集", (Visual)dict["appbar_email_hardedge"], 2));
            foundIcons.Add(new MetroIcon("磁盘监控", (Visual)dict["appbar_cloud_upload"], 1));
            foundIcons.Add(new MetroIcon("性能监控", (Visual)dict["appbar_graph_line_up"], 1));
            foundIcons.Add(new MetroIcon("服务监控", (Visual)dict["appbar_home"], 0));
            foundIcons.Add(new MetroIcon("WEB站点监控", (Visual)dict["appbar_monitor"], 2));
            foundIcons.Add(new MetroIcon("数据库监控", (Visual)dict["appbar_people_status"], 1));
            foundIcons.Add(new MetroIcon("数据库健康巡检", (Visual)dict["appbar_alert"], 1));
            foundIcons.Add(new MetroIcon("日志采集", (Visual)dict["appbar_email_hardedge"], 2));
            foundIcons.Add(new MetroIcon("磁盘监控", (Visual)dict["appbar_cloud_upload"], 2));
            foundIcons.Add(new MetroIcon("性能监控", (Visual)dict["appbar_graph_line_up"], 1));
            clientServerInfo.PluginStatus = foundIcons;

            ThreadPool.QueueUserWorkItem(new WaitCallback(ConstructModel), clientServerInfo);
        }

        private void ConstructModel(object state)
        {
            Thread.Sleep(5 * 1000);

            var clientServerInfo = state as ClientServerInfo;         
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                this.Model = clientServerInfo;
                this.IsBusy = false;
            }), DispatcherPriority.Send);
        }
    }

    internal sealed class MetroIcon
    {
        public MetroIcon(string name, Visual visual, int status)
        {
            Name = name;
            Visual = visual;
            Status = status;
        }

        public string Name { get; private set; }
        public Visual Visual { get; private set; }
        public int Status { get; private set; }
    }

    internal sealed class ClientServerInfo
    {
        public ClientServer BasicServerInfo { get; set; }
        public List<MetroIcon> PluginStatus { get; set; }

        public ClientServerInfo()
        {
            this.PluginStatus = new List<MetroIcon>();
        }
    }
}
